package md.mru.core.common;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Contains methods useful to convert one object to another
 *
 * @author sergiu.marin
 * @since 15.11.2013
 */
@Component(value = "dtoMapper")
public class BaseDtoMapper {

    @Autowired
    Mapper mapper;

    /**
     *  Method used to recursively copy data
     *  from one object to another.
     *
     * @param obj Object    - source data object
     * @param clazz Class   - target data object
     * @param <T>           - type of returned object
     * @return target data object populated with data from source data object
     */
    public <T> T map(Object obj, Class<T> clazz) {
        return mapper.map(obj, clazz);
    }

    /**
     * Method used to recursively copy data
     * from one list of objects to another.
     *
     * @param list  - source data objects list
     * @param clazz - target data objects list
     * @param <T>   - type of returned object
     * @return target data objects list populated
     * with data from source data objects list

     */
    public <T> List<T> mapList(List list, Class<T> clazz) {
        List<T> mappedObjectList = new ArrayList<T>(list.size());
        for (Object object : list)
            mappedObjectList.add(this.map(object, clazz));

        return mappedObjectList;
    }
}
